<?php session_start();
include("../../Connections/connect_admin.php");
if($_POST['task']=="add")
{
	$topic=intval($_POST["topic"]);
	$name=$_POST["votename"];
	$content=$_POST["content"];
	$user=$_SESSION["username"];
	$query="INSERT INTO vote(TopicID, VoteName, Content,CreatedUser) VALUES($topic,'$name','$content','$user')";
	
	if(mysql_query($query))
	{
		$res=mysql_query("SELECT max(v.VoteID) AS VoteID FROM vote v");
		$row=mysql_fetch_assoc($res);
		$max=$row["VoteID"];
		
		foreach($_SESSION['addvote'] as $k=>$item)
		{	
		$query2="INSERT INTO votedetail(VoteID, Content) VALUES($max,'$item')";
		mysql_query($query2); 
	
		}
		header('location:../Vote.php?e=0');
		
	}
	else
	{
		header('location:../Vote.php?e=1');
		
	}
}
else if($_POST['task']=="getbyid")
{
	unset($_SESSION['addvote']);
	unset($_SESSION['delvote']);
	$id=intval($_POST['voteid']);
	$query="SELECT v.VoteID, v.TopicID, v.VoteName, v.Content, v.IsEnd, v.EndDate,v.CreatedDate, v.CreatedUser FROM vote v WHERE v.VoteID=$id ";
	$result=mysql_query($query);
	$vote=mysql_fetch_assoc($result);
	$query2="SELECT * FROM votedetail v WHERE v.VoteID=$id ORDER BY VoteDetailID";
	$result2=mysql_query($query2);
	$query3="SELECT sum(v.AgreeNum) AS Total FROM votedetail v WHERE v.VoteID=$id";
	$result3=mysql_query($query3);
	$total=mysql_fetch_assoc($result3);
	$i=0;
	while($row=mysql_fetch_assoc($result2))
	{
		$percent=0;
		if($total['Total']>0)
		{
		$percent=round(($row['AgreeNum']/$total['Total'])*100,2);
		}
		
		$details[$i]=array(
						   'id'=>$row['VoteDetailID'],
						   'voteid'=>$row['VoteID'],
						   'content'=>$row['Content'],
						   'agreenum'=>$row['AgreeNum'],
						   'percent'=>$percent
									  );
		$i++;
	}
	$response=array(
					'topic'=>$vote['TopicID'],
					'isend'=>$vote['IsEnd'],
					'name'=>$vote['VoteName'],
					'content'=>$vote['Content'],
					'total'=>$total['Total'],
					'detail'=>$details
					);
	echo json_encode($response);
}
else if($_POST['task']=="delete")
{
	$user=$_SESSION["username"];
	$id=intval($_POST['voteid']);
	$query="UPDATE vote SET IsDeleted = 1,DeletedDate = NOW(),DeletedUser = '$user' WHERE VoteID=$id";
	if(mysql_query($query))
	{
		$response=array(
						'ok'=>true,
						'msg'=>'Xóa bình chọn Thành Công'
						);
		echo json_encode($response);
	}
	else
	{
		$response=array(
						'ok'=>false,
						'msg'=>'Xóa bình chọn Thất bại'
						);
		echo json_encode($response);
	}
}
else if($_POST['task']=="update")
{
	$voteid=intval($_POST["voteid"]);
	$topic=intval($_POST["topic"]);
	$name=$_POST["votename"];
	$content=$_POST["content"];
	$query="UPDATE vote SET TopicID = $topic,VoteName = '$name',Content = '$content'";
	if($_POST["isend"]=='end')
	{
		$query.=", IsEnd=1, EndDate= NOW() ";
	}
	else
	{
		$query.=", IsEnd=0, EndDate= Null ";
	}
	$query.=" WHERE VoteID=$voteid";
	
	if(mysql_query($query))
	{
		foreach($_SESSION['addvote'] as $k=>$item)
		{	
		$query2="INSERT INTO votedetail(VoteID, Content) VALUES($voteid,'$item')";
		mysql_query($query2); 
	
		}
		foreach($_SESSION['delvote'] as $k=>$item)
		{	
		$query3="DELETE FROM votedetail WHERE VoteDetailID=$item ";
		mysql_query($query3); 
	
		}
		header('location:../Vote.php?e=2');
		
	}
	else
	{
		header('location:../Vote.php?e=3');
		
	}

}
else if($_POST['task']=='adddetail')
{
	$id=mt_rand(100,10000);
	$content=$_POST['content'];
	$_SESSION['addvote'][$id]=$content;
	echo json_encode( array('ok'=>true,'id'=>$id));
}
else if($_POST['task']=='deldetail')
{
	$id=intval($_POST['id']);
	if($id>=100)//new item
	{
		unset($_SESSION['addvote'][$id]);
	}
	else
	{
	$_SESSION['delvote'][$id]=$id;
	}
	echo json_encode( array('ok'=>true));
}
else if($_POST['task']=='reset')
{
	unset($_SESSION['addvote']);
	unset($_SESSION['delvote']);
}
?>